import numpy as np
import pandas as pd
import pyecharts

class ChipotleAna:

    path = r'C:\Users\阿佑i\test4.21\test_7\chipotle.csv'


    def __init__(self):

        #self.path = 'C:/Users/Administrator/pandas_exercises-c1/01_Getting_&_Knowing_Your_Data/Chipotle/chipotle.csv'

        self.text = pd.read_csv(self.path, sep = '\t')

#bar 
    def echarts_bar(self, series, bar_name = 'test', name = 'test_name' ):

        bar = pyecharts.Bar(bar_name)

        axis0 = series.index

        axis1 = series.values.ravel()

        bar.add(name, axis0, axis1, is_label_show=True, is_datazoom_show=True)

        return bar

#pie
    def echarts_pie(self, series, pie_name = 'test', name = 'test_name'):

        pie = pyecharts.Pie(pie_name)

        attr = series.index

        value = series.values.ravel()

        pie.add(name, attr, value, is_label_show = True, radius = [0, 50] , center = [50, 70])

        return pie

    
# 1每种商品卖了多少个

        
    def how_many_are_sold_per_item(self):#每个商品卖了多少件

        count_info = self.text.groupby('item_name').sum()

        count_info = count_info.drop('order_id', axis = 1)

        return count_info

    def bar_how_many_are_sold_per_item(self):

        return self.echarts_bar(self.how_many_are_sold_per_item(),bar_name = '每种商品卖出个数', name = '数量')

#2每种商品卖了多少钱
    def how_much_are_sold_per_item(self):#第个商品卖了多少钱

        x = self.text.quantity*(self.text.item_price.str.replace('$','').astype('float64'))
        
        df1 = self.text.copy()
        
        df1['price'] = round(x, 2)
        
        df1 = df1.groupby('item_name').sum()
        
        return round(df1['price'], 2)

    def bar_how_much_are_sold_per_item(self):

        return self.echarts_bar(self.how_much_are_sold_per_item(), bar_name = '每种商品总收入', name = '总收入')


#3每种商品卖了多少个
    def item_name_count(self):

        item_quantity = self.text.groupby('item_name').sum().drop('order_id', axis = 1)

        item_name = item_quantity.sort_values('quantity')

        return item_name

    def bar_item_name_count(self):

        return self.echarts_bar(self.item_name_count(), bar_name = '每个商品卖出的数量')

#4每个订单价钱
    def order_price(self):

        x = self.text.quantity*(self.text.item_price.str.replace('$','').astype('float64'))
        
        df1 = self.text.copy()
        
        df1['price'] = round(x, 2)
        
        df1 = df1.groupby('order_id').sum()
        
        return round(df1['price'], 2).head(10)

    def bar_order_price(self):

        return self.echarts_bar(self.order_price(), bar_name = '每个订单价钱', name = '每单收入')

#5流水比例

    def item_price_percentage(self):

        x = self.text.quantity*(self.text.item_price.str.replace('$','').astype('float64'))
        
        df1 = self.text.copy()
        
        df1['price'] = round(x, 2)
        
        df1 = df1.groupby('item_name').sum()
        
        return round(df1['price'], 2).head(10)

    def pie_item_price_percentage(self):

        return self.echarts_pie(self.item_price_percentage(), pie_name = '流水展示', name = '流水占比')


#6Chiken_Bowl每种配料选择的次数及比例

    def choice_description_percentage(self):

        text_group = self.text.groupby(['item_name', 'choice_description']).sum()

        text_Chickem_Bowl_values = text_group.loc['Chicken Bowl'].sort_values('quantity', ascending = False)

        text_Chicken_Bowl_quantity_values = text_Chickem_Bowl_values.drop(['order_id'], axis = 1)

        return text_Chicken_Bowl_quantity_values.head(10)

    def pie_choice_description_percentage(self):

        return self.echarts_pie(self.choice_description_percentage(), pie_name = 'Chiken Bowl配料图', name = '配料占比')

    


        
